package com.cn.dim.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 




import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Pages;
import com.cn.dim.pojo.Prop;
import com.cn.dim.service.ChkpointService;
import com.cn.dim.service.ChktaskService;
import com.cn.dim.service.InfoService;
import com.cn.dim.service.PropService;
import com.cn.dim.utils.StringUtil;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/h5prop")
public class H5PropController {

	@Resource
	private PropService propService;
	@Resource
	private InfoService infoService;
	@Resource
	private ChkpointService chkpointService;
	@Resource
	private ChktaskService chktaskService;
	
	
	
	
	
	@RequestMapping(value ="/queryChktaskdetail")
	@ResponseBody
	public Map<String,Object> queryChktaskdetail(HttpServletRequest request,String id){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", id);
		param.put("businame", "chktask");
		try {
			List<	Map<String,Object>> taskdetail=chktaskService.queryChktaskdetail(param);
			List<	Map<String,Object>> files=chktaskService.queryfiles(param);
			taskdetail.get(0).put("filelist", files);
			reMap.put("taskdetail", taskdetail.get(0));
			reMap.put("status", "success");
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	
	@RequestMapping(value ="/insertchktask")
	@ResponseBody
	public Map<String,Object> insertchktask(HttpServletRequest request,String chkpointid,String datarecord,String iserror,String chkdes,String filelist){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		List<	Map<String,Object>> ja=(List)JSONArray.fromObject(filelist);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String uuid=StringUtil.makeUUID();
		param.put("chkpointid", chkpointid);
		param.put("chkuserid", "02");
		param.put("datarecord", datarecord);
		param.put("id", uuid);
		param.put("chktime", sdf.format(date));
		param.put("iserror", iserror);
		param.put("chkdes", chkdes);
		param.put("isdel", 0);
		for(	Map<String,Object> j:ja){
			j.put("id", StringUtil.makeUUID());
			j.put("businame", "chktask");
			j.put("busitableid",uuid);
			j.put("filetype", 1);
			j.put("createuserid", "02");
			j.put("createtime", sdf.format(date));
		}
		try {
			chktaskService.insertchktask(param);
			chktaskService.insertfilelist(ja);
			
			reMap.put("status", "success");
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	
	
	@RequestMapping(value ="/queryproptypelistdetail")
	@ResponseBody
	public Map<String,Object> queryproptypelistdetail(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			List<Map<String,Object>> proptypelist =propService.queryproptypelistdetail(param);
			for(Map<String,Object> p:proptypelist){
				param.put("proptypeid", p.get("id"));
				List<Map<String,Object>> prop=propService.querypropbytypeid(param);
				p.put("props", prop);
			}
			reMap.put("chkinputitemlist",proptypelist);
			reMap.put("status", "success");
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	
	
	@RequestMapping(value ="/queryChkinputitemlistbyckpid")
	@ResponseBody
	public Map<String,Object> queryChkinputitemlistbyckpid(HttpServletRequest request,String chkpointid){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("chkpointid", chkpointid);
		try {
			List<Map<String,Object>> chkinputitemlist =chkpointService.queryChkinputitemlist(param);
			reMap.put("chkinputitemlist",chkinputitemlist);
			reMap.put("status", "success");
		} catch (Exception e) { 
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryChktasklist")
	@ResponseBody
	public Map<String,Object> queryChktasklist(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			List<Map<String,Object>> chktasklist =chktaskService.queryChktasklist(param);
			reMap.put("chktasklist",chktasklist);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryChkpointlist")
	@ResponseBody
	public Map<String,Object> queryChkpointlist(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			List<Map<String,Object>> chkpoinlist =chkpointService.queryChkpointlist(param);
			reMap.put("chkpoinlist",chkpoinlist);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/queryinfo")
	@ResponseBody
	public Map<String,Object> queryinfo(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		
		try {
			List<Map<String,Object>> infolist =infoService.queryInfolist(param);
			reMap.put("infolist",infolist);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	
	
	@RequestMapping(value ="/queryProp",method = RequestMethod.GET)
	@ResponseBody
	//Pages是自己定义的页面实体�?
	public Map<String,Object> queryProp(HttpServletRequest request,Prop p,Pages pages,String stime,String etime){
		Map<String,Object> param=new HashMap<>();//定义查询参数
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		//参数p是列表页中以查询条件数据临时组成的过渡prop对象
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());
		param.put("prop", p);
		param.put("stime", stime);//开始时间
		param.put("etime", etime);//截止时间
		
		try {
			Page<Map<String,Object>> rList = (Page)propService.queryProp(param);
			//Page对象认为是一个以键值对保存的一行记录的多条记录的集合，
			for(Map m:rList){
				
				Map<String,Object> queryProptypenamepara=new HashMap<>();//定义queryAuth方法查询参数
				queryProptypenamepara.put("proptypeid", m.get("proptypeid"));

				List<Map<String,Object>> proptypenames=propService.queryProptypeName(queryProptypenamepara);//返回值的List中只有一个Map元素
				String proptypenameStr="";
				for(Map proptypename:proptypenames){//实际上只循环一遍
					proptypenameStr=proptypenameStr+(String)proptypename.get("proptypename");
				}
				m.put("proptypenameStr", proptypenameStr);//在每行记录中补充1个用于显示的键值对
			}
			System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	//宣传类别（板块）下拉框获取函数；
	@RequestMapping(value ="/queryProptype",method = RequestMethod.POST)
	@ResponseBody 
	public List<Map<String,Object>> queryProptype(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();//查询不需要参数，这里作为一个空参数传过去；
		Map<String,Object> reMap=new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			list =propService.queryProptypeh5(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println(list);
		return list;
	}
	
	
	/**
	 * 根据id查询propid
	 * @param request
	 * @param id
	 * @return
	 * @author 任晓雨
	 * @date 2019年1月3日 下午2:32:18
	 */
	@RequestMapping(value ="/querypropbyId",method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> querypropbyId(HttpServletRequest request,String id){
		Map<String,Object> param=new HashMap<>();//查询不需要参数，这里作为一个空参数传过去；
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", id);
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			list =propService.querypropbyId(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}

	@RequestMapping(value ="/insertProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertProp(Prop p)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		String propid=com.cn.dim.utils.StringUtil.makeUUID();
		p.setId(propid);
		p.setIsdel("0");
		
		try 
		{
			int flag = propService.insertProp(p);
			if(flag>0)
			{
				reMap.put("message", "插入成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateProp(Prop p,String propid)  
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		p.setId(propid);//实体对象传来的并不含有id的值，这里要使用页面传过来propid值进行填充
		try 
		{
			int flag = propService.updateProp(p);
			if(flag>0)
			{
				reMap.put("message", "修改成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}

	@RequestMapping(value ="/delProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delProp(HttpServletRequest request,String propid)   //插入�?条单�?的prop记录
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        Map<String,Object> m=new HashMap<>();//构造service接口中delProp方法的参数
        m.put("propid", propid);
		try 
		{
			int flag = propService.delProp(m);
			if(flag>0)
			{
				reMap.put("message", "删除成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/queryPropById")
	@ResponseBody
	public Map<String,Object> queryPropById(HttpServletRequest request,String propid){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		m.put("propid", propid);
		try {
			Prop prop =propService.queryPropById(m);
			reMap.put("data", prop);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
